<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Client;
use App\Models\Cartype;
use App\Models\CarBrand;
use App\Models\CarSerie;
use App\Models\Wxuser;
use App\Repos\ClientRepo;
use App\Imports\ClientImport;
use App\Exports\ClientExport;
use Maatwebsite\Excel\Facades\Excel;

class ClientController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = [];
        $query = Client::query();
        if($request->keyword) {
            $query->where('title','like','%'.$request->keyword.'%');
        }
        $list = $query->paginate(20);
        return view('admin.client.index',compact('list'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cartypes = Cartype::get();
        $brands   = CarBrand::get();
        $wxusers  = Wxuser::get();
        return view('admin.client.add',compact('cartypes','brands','wxusers'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rs['status'] = 'danger';
        $rs['msg']    = '操作失败';
        $data = $request->except(['_token']);      
        $data['code'] = date('YmdHis').str_random(4);
        $flag = ClientRepo::store($data);
        if($flag['status']) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return redirect('zadmin/clients')->with('rs',$rs);
        }
        $rs['msg'] = $flag['msg'];
        return back()->with('rs',$rs);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Client::find($id);
        $cartypes = Cartype::get();
        $brands   = CarBrand::get();
        $wxusers  = Wxuser::get();
        return view('admin.client.edit',compact('data','cartypes','brands','wxusers'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except('_token','_method');
        //$data['updater'] = session('admin')->username;
        $rs   = Client::where('id',$id)->update($data);
        if($rs) {
            return redirect('zadmin/clients');
        }
        return back()->withInput();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs['status'] = 'danger';
        $flag = ClientRepo::destroy($id);
        if($flag['status']==false) {
            $rs['msg'] = $flag['msg'];
            return back()->with('rs',$rs);
        }
        $rs['status'] = 'success';
        $rs['msg'] = '操作成功';
        return back()->with('rs',$rs);
    }

    public function import(Request $request)
    {
        //Client::delete();
        Excel::import(new ClientImport, request()->file('excel'));
        return back();

    }


    public function export(Request $request)
    {
        //dd(1);
        $param = [];
        if($request->client_id) {
            $param['keyword'] = (int)$request->keyword;
        }
        return Excel::download(new ClientExport($param), '客户信息.xlsx');   
    }
}
